/*
 * FortressSprite - sprites for fortress pieces
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#if flash9
import flash3f.Images;
#end
import Type;

class FortressSprite
{
	
	#if flash9
	public var fort : FortressData;
	public var image : IsoBitmap;

	public static var flags : Array<IsoBitmap> = new Array();
	public static var icycle : Int = 0;
	public static var sprites : Array<FortressSprite> = new Array();
	#end
	
	public function new(fort : FortressData)
	{
		this.fort = fort;
	
		if (fort.mystyle==1) // wall
		{
			#if flash9
			image = new IsoBitmap(Images.getdata(0xBEEFBF));
			image.offx = 0;
			image.offy = -14;
			#end
		}
		
		if (fort.mystyle==2) // castle
		{
			#if flash9
			image = new IsoBitmap(Images.getdata(0xFAFA01));
			image.offx = 0;
			image.offy = -31;
			#end
		}

		if (fort.mystyle==3) // tower
		{
			#if flash9
			image = new IsoBitmap(Images.getdata(0x020202));
			image.offx = 0;
			image.offy = -31;
			#end
		}

		#if flash9
		IsoManager.add(image);
		image.movep(IsoMetrics.tiletopix_xy(fort.tx,fort.ty));
		sprites.push(this);
		#end

	}

	// updates should check to see if the fortdata is gone

	public static function start()
	{
		
	}
	
	public static function end()
	{
		
	}

	public static function up()
	{
		for (n in sprites)
		{
			if (n.fort==null)
			{
				#if flash9
				IsoManager.remove(n.image);
				if (n.fort.iscastle)
				SFX.castledeath = true;
				else
				SFX.fortdeath = true;
				#end
				sprites.remove(n);
				updateflags();
			}
		}
		animateflags();
	}

	public static function refreshall()
	{

		for (n in sprites)
		{
			#if flash9
			IsoManager.remove(n.image);
			#end
			sprites.remove(n);
		}

		sprites = new Array();
		
		for (p in Graph.iter(Game.cur.map))
		{
			if (p.fort!=null)
			{
				sprites.push(new FortressSprite(p.fort));
			}
		}
	}
	
	public static function flatten()
	{
		#if flash9
		for (n in sprites)
		{
			if (n.fort.shooter==null) // no weapon == wall
			{
				n.image.bitmapData.dispose();
				n.image.bitmapData = Images.getdata(0xBEEFBF);
			}
		}
		#end
	}
		
	public static function unflatten()
	{
		#if flash9
		for (n in sprites)
		{
			if (n.fort.shooter==null) // no weapon == wall
			{
				n.image.bitmapData.dispose();
				n.image.bitmapData = Images.getdata(0xBEEFBE);
			}
		}
		#end
	}
		
	public static function updateflags()
	{
		#if flash9
		for (n in flags)
		{
			IsoManager.remove(n);
			n.bitmapData.dispose();
			n = null;
		}

		flags = new Array();
		
		for (c in sprites)
		{
			if (c.fort !=null && c.fort.iscastle)
			{
				if(c.fort.castlecontrol)
				{
					c.image.bitmapData = Images.getdata(0xFAFA01);
					var flag : IsoBitmap = new IsoBitmap(Images.getdata(0xF00F00));
					IsoManager.add(flag);
					flag.offx = 13;
					flag.offy = -30;
					flag.movep(IsoMetrics.tiletopix_xy(c.fort.tx,c.fort.ty));
					flags.push(flag);
				}
				else
				{
					c.image.bitmapData = Images.getdata(0xFAFA00);			
				}
			}
		}
		#end
	}

	public static function animateflags()
	{
		#if flash9
		if (Type.typeof(icycle)==TInt)
		{
			if(icycle==3)
			{
				for (sub in flags)
					sub.bitmapData=Images.getdata(0xF00F00);
			}
			else if(icycle==6)
			{
				for (sub in flags)
					sub.bitmapData=Images.getdata(0xF00F02);
			}
			else if(icycle==9)
			{
				for (sub in flags)
					sub.bitmapData=Images.getdata(0xF00F01);
				icycle=-1;
			}
			icycle++;
		}
		#end
	
	}

	public function drawangle()
	{
		#if flash9
		
		if (fort.shooter != null)
		{
		
			var npang = fort.shooter.angle/3.14;
			var style = fort.mystyle;
			var baseimage : Int;
			if (fort.shooter.weapon==1)
			{
				baseimage = 0x020200;
			}
			else if (fort.shooter.weapon==2)
			{
				baseimage = 0x020300;
			}
			else if (fort.shooter.weapon==3)
			{
				baseimage = 0x020400;
			}
			else if (fort.shooter.weapon==4)
			{
				baseimage = 0x020500;
			}
			
			if ((npang<(-7/8)) || (npang>=(7/8)))
			{ // down
				image.bitmapData = Images.getdata(baseimage+7);
			}
			if (npang<(-5/8))
			{ // lower-right
				image.bitmapData = Images.getdata(baseimage+6);			
			}
			else if (npang<(-3/8))
			{ // right
				image.bitmapData = Images.getdata(baseimage+5);			
			}
			else if (npang<(-1/8))
			{ // upper-right
				image.bitmapData = Images.getdata(baseimage+4);			
			}
			else if (npang<(1/8))
			{ // up
				image.bitmapData = Images.getdata(baseimage+3);			
			}
			else if (npang<(3/8))
			{ // upper-left
				image.bitmapData = Images.getdata(baseimage+2);			
			}
			else if (npang<(5/8))
			{ // left
				image.bitmapData = Images.getdata(baseimage+9);			
			}
			else if (npang<(7/8))
			{ // lower-left
				image.bitmapData = Images.getdata(baseimage+8);			
			}
			
		}
		#end	
	}
		
}
